package com.first.dao.impl;

import com.first.dao.OOrderDao;
import com.first.entity.OOrder;
import com.first.entity.OOrderWj;
import com.first.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class OOrderDaoImpl implements OOrderDao {
    @Override
    public String updateBookCount(OOrder oOrder) {
        String sql = "select bookid,ocount from o_order_wj WHERE o_orderid=?";
        List<Map> map = DBUtil.queryListMap(sql, oOrder.getO_orderid());
        //通过点单好获取book信息，进行书籍库存的更改
        String bookname = "";
        for (Map map1 : map) {
            int bookid = (int) map1.get("bookid");
            int count1 = (int) map1.get("ocount");
            String sql1 = "select count from book where bookid=?";
            List<List> lists = DBUtil.queryList(sql1, bookid);
            int count2 = (int) lists.get(0).get(0);
            if (count2 > count1) {
                int count = count2 - count1;
                String sql2 = "update book set count=? where bookid=?";
                int update = DBUtil.update(sql2, count, bookid);
            } else {
                bookname += map1.get("bookname");
            }
        }
        String sql3 = "update o_order set status=1,o_orderoutdate=? ,w_name=? where o_orderid=?";
        int update = DBUtil.update(sql3, oOrder.getO_orderoutdate(), oOrder.getW_name(), oOrder.getO_orderid());
        if (!"".equals(bookname)) {
            return bookname;
        }
        return "";
    }

    @Override
    public List<Map> queryAllOutOrder(OOrder oOrder) {
        StringBuilder sb = new StringBuilder("select * from o_order where status=0");
        if (oOrder.getO_name() != null && !"".equals(oOrder.getO_name())) {
            sb.append(" and o_name like '%" + oOrder.getO_name() + "%'");

        } else if (oOrder.getO_orderid() != null && !"".equals(oOrder.getO_orderid())) {
            sb.append(" and o_orderid like '%" + oOrder.getO_orderid() + "%'");

        } else if (oOrder.getO_orderindate() != null && !"".equals(oOrder.getO_orderindate())) {
            sb.append(" and o_orderindate like '%" + oOrder.getO_orderindate() + "%'");

        }
        sb.append(" ORDER BY o_orderindate DESC limit ?,?");
        List<Map> list = DBUtil.queryListMap(sb.toString(), (oOrder.getPage() - 1) * oOrder.getLimit(), oOrder.getLimit());
        if (list.size() > 0) {
            return list;
        }
        return null;
    }


    @Override
    public int queryAllOutOrderCount(OOrder oOrder) {
        StringBuilder sb = new StringBuilder("select count(*) from o_order where status=0");
        if (oOrder.getO_name() != null && !"".equals(oOrder.getO_name())) {
            sb.append(" and o_name like '%" + oOrder.getO_name() + "%'");

        } else if (oOrder.getO_orderid() != null && !"".equals(oOrder.getO_orderid())) {
            sb.append(" and o_orderid like '%" + oOrder.getO_orderid() + "%'");

        } else if (oOrder.getO_orderindate() != null && !"".equals(oOrder.getO_orderindate())) {
            sb.append(" and o_orderindate like '%" + oOrder.getO_orderindate() + "%'");

        }
        sb.append(" ORDER BY o_orderoutdate DESC ");
        List<List> lists = DBUtil.queryList(sb.toString());
        Object o = lists.get(0).get(0);
        int i = Integer.parseInt(o + "");
        return i;
    }

    /**
     * 添加销售单
     *
     * @param OOrder
     * @return
     */
    @Override
    public int addOOrder(OOrder OOrder) {

        String sql = "insert into o_order values(?,?,null,?,0,null,null)";
        int i = DBUtil.update(sql, OOrder.getO_orderid(), OOrder.getO_name(), OOrder.getO_orderindate());
        return i;
    }

    /**
     * 将出库单驳回
     *
     * @param OOrder
     * @return
     */
    @Override
    public int bhOOrder(OOrder OOrder) {
        String sql = "UPDATE o_order b SET b.status=2,b.bh=?,b.w_name=? WHERE b.o_orderid=?";
        int update = DBUtil.update(sql, OOrder.getBh(), OOrder.getW_name(), OOrder.getO_orderid());
        return update;
    }

    /**
     * 查询驳回的销售单
     *
     * @return
     */
    @Override
    public List<Map> oorderStatus2(OOrder oOrder) {
        StringBuilder sb = new StringBuilder("select * from o_order where status=2");
        if (oOrder.getW_name() != null && !"".equals(oOrder.getW_name())) {
            sb.append(" and w_name like '%" + oOrder.getW_name() + "%'");

        } else if (oOrder.getO_name() != null && !"".equals(oOrder.getO_name())) {
            sb.append(" and o_name like '%" + oOrder.getO_name() + "%'");

        } else if (oOrder.getO_orderid() != null && !"".equals(oOrder.getO_orderid())) {
            sb.append(" and o_orderid like '%" + oOrder.getO_orderid() + "%'");

        } else if (oOrder.getO_orderindate() != null && !"".equals(oOrder.getO_orderindate())) {
            sb.append(" and o_orderindate like '%" + oOrder.getO_orderindate() + "%'");

        }
        sb.append(" ORDER BY o_orderindate DESC limit ?,?");
        List<Map> list = DBUtil.queryListMap(sb.toString(), (oOrder.getPage() - 1) * oOrder.getLimit(), oOrder.getLimit());
        if (list.size() > 0) {
            return list;
        }
        return null;
    }


    @Override
    public int oorderStatus2Count(OOrder oOrder) {
        StringBuilder sb = new StringBuilder("select count(*) from o_order where status=2");
        if (oOrder.getW_name() != null && !"".equals(oOrder.getW_name())) {
            sb.append(" and w_name like '%" + oOrder.getW_name() + "%'");

        } else if (oOrder.getO_name() != null && !"".equals(oOrder.getO_name())) {
            sb.append(" and o_name like '%" + oOrder.getO_name() + "%'");

        } else if (oOrder.getO_orderid() != null && !"".equals(oOrder.getO_orderid())) {
            sb.append(" and o_orderid like '%" + oOrder.getO_orderid() + "%'");

        } else if (oOrder.getO_orderindate() != null && !"".equals(oOrder.getO_orderindate())) {
            sb.append(" and o_orderindate like '%" + oOrder.getO_orderindate() + "%'");

        }
        sb.append(" ORDER BY o_orderoutdate DESC ");
        List<List> lists = DBUtil.queryList(sb.toString());
        Object o = lists.get(0).get(0);
        int i = Integer.parseInt(o + "");
        return i;
    }

    /**
     * 添加销售订单详情表
     *
     * @param oOrderWj
     * @return
     */
    @Override
    public double addOOrderWj(OOrderWj oOrderWj) {
        String s = "select outprice from book b where b.bookid=?";
        List<List> lists = DBUtil.queryList(s, oOrderWj.getBookid());
        Object o = lists.get(0).get(0);
        double i1 = Double.parseDouble(o + "");
        String sql = "insert into o_order_wj values(?,?,?,?)";
        int i = DBUtil.update(sql, oOrderWj.getO_orderid(), oOrderWj.getBookid(), oOrderWj.getOcount(), i1 * oOrderWj.getOcount());
        return i1 * oOrderWj.getOcount();
    }

    /**
     * 查询销售订单详情
     *
     * @param oOrderWj
     * @return
     */
    @Override
    public List<Map> getOOrderWJ(OOrderWj oOrderWj) {
        String sql = "SELECT o.bookid,o_orderid,bookname,author,press,b.count,o.ocount,o_price,outprice FROM o_order_wj o,book b where o_orderid=? and b.bookid=o.bookid";
        List<Map> maps = DBUtil.queryListMap(sql, oOrderWj.getO_orderid());
        return maps;
    }

    /**
     * 查询所有出库的书籍
     *
     * @param oOrder
     * @return
     */
    @Override
    public List<Map> queryAllStatus1(OOrder oOrder) {
        StringBuilder sb = new StringBuilder("select * from o_order where status=1");
        if (oOrder.getW_name() != null && !"".equals(oOrder.getW_name())) {
            sb.append(" and w_name like '%" + oOrder.getW_name() + "%'");

        } else if (oOrder.getO_name() != null && !"".equals(oOrder.getO_name())) {
            sb.append(" and o_name like '%" + oOrder.getO_name() + "%'");

        } else if (oOrder.getO_orderid() != null && !"".equals(oOrder.getO_orderid())) {
            sb.append(" and o_orderid like '%" + oOrder.getO_orderid() + "%'");

        } else if (oOrder.getO_orderoutdate() != null && !"".equals(oOrder.getO_orderoutdate())) {
            sb.append(" and o_orderoutdate like '%" + oOrder.getO_orderoutdate() + "%'");

        }
        sb.append(" ORDER BY o_orderoutdate DESC limit ?,?");
        List<Map> list = DBUtil.queryListMap(sb.toString(), (oOrder.getPage() - 1) * oOrder.getLimit(), oOrder.getLimit());
        if (list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public int queryAllStatus1Count(OOrder oOrder) {
        StringBuilder sb = new StringBuilder("select count(*) from o_order where status=1");
        if (oOrder.getW_name() != null && !"".equals(oOrder.getW_name())) {
            sb.append(" and w_name like '%" + oOrder.getW_name() + "%'");

        } else if (oOrder.getO_name() != null && !"".equals(oOrder.getO_name())) {
            sb.append(" and o_name like '%" + oOrder.getO_name() + "%'");

        } else if (oOrder.getO_orderid() != null && !"".equals(oOrder.getO_orderid())) {
            sb.append(" and o_orderid like '%" + oOrder.getO_orderid() + "%'");

        } else if (oOrder.getO_orderoutdate() != null && !"".equals(oOrder.getO_orderoutdate())) {
            sb.append(" and o_orderoutdate like '%" + oOrder.getO_orderoutdate() + "%'");

        }
        sb.append(" ORDER BY o_orderoutdate DESC ");
        List<List> lists = DBUtil.queryList(sb.toString());
        Object o = lists.get(0).get(0);
        int i = Integer.parseInt(o + "");
        return i;
    }

    /**
     * 修改驳回销售订单的数据
     *
     * @param OOrderWj
     * @return
     */
    @Override
    public int updateReject(OOrderWj OOrderWj) {

        String sql = "update o_order_wj set ocount=?,o_price=? where o_orderid=? and bookid=?";
        int i = DBUtil.update(sql, OOrderWj.getOcount(), OOrderWj.getOcount() * OOrderWj.getOutprice(), OOrderWj.getO_orderid(), OOrderWj.getBookid());

        return i;

    }

    /**
     * 将驳回的订单重新提交
     *
     * @param oOrder
     * @return
     */
    @Override
    public int updateStatus0(OOrder oOrder) {

        String sql = "UPDATE o_order set status=0 where o_orderid=? ";
        int i = DBUtil.update(sql, oOrder.getO_orderid());
        return i;
    }

}
